package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.AdminService;
import com.atguigu.service.HouseBrokerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    @Reference
    private AdminService adminService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @RequestMapping("/create")
    public String create(Long houseId, Model model){
        //需要带房屋ID过去
        model.addAttribute("houseId",houseId);
        //需要查询到所有用户的信息列表,用于经纪人下拉框.
        List<Admin> adminList = adminService.findAll();
        model.addAttribute("adminList",adminList);
        return "houseBroker/create";
    }

    @RequestMapping("/save")
    public String save(Long houseId,Long brokerId){
        //此borkerId是前端选的adminList列表里的admin的id
        //我们需要根据两个形参获取到HouseBroker对象的数据并封装 然后添加到数据库
 /*       //房源id
        private Long houseId;
        //经纪人id
        private Long brokerId;
        //经纪人名称
        private String brokerName;
        //经纪人头像
        private String brokerHeadUrl;*/

        //根据HouseBroker的对象属性可知,我们还缺两个数据,一个是NAME,一个是HeadUrl ,
        // 都需要通过brokerId去查询admin表获取到
        Admin admin = adminService.getById(brokerId);
        HouseBroker houseBroker = new HouseBroker();
        houseBroker.setHouseId(houseId);
        houseBroker.setBrokerId(brokerId);
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        houseBrokerService.insert(houseBroker);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id ,Model model){
        //此id是经纪人表的主键
        //根据ID查询到经纪人表的信息封装
        HouseBroker houseBroker = this.houseBrokerService.getById(id);
        model.addAttribute("houseBroker",houseBroker);
        //修改页面除了本表信息,还需要所有经纪人也就是admin列表
        List<Admin> adminList = adminService.findAll();
        model.addAttribute("adminList",adminList);
        return "houseBroker/edit";
    }
    @RequestMapping("/update")
    public String update(HouseBroker houseBroker){
        //传入的数据有houseBroker的主键id  以及修改后的经纪人主键的brokerId
        //需要根据经纪人主键Id查询并补齐表中的经纪人姓名跟头像url
        Admin admin = this.adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        this.houseBrokerService.update(houseBroker);
        return "common/successPage";
    }

    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId,@PathVariable Long id){
        this.houseBrokerService.delete(id);
        return "redirect:/house/"+houseId;
    }

}
